package com.shengyuan.manage_os.old.ying.erpm;

import java.util.ArrayList;
import java.util.List;
import org.json.JSONObject;

import com.shengyuan.manage_os.R;
import com.shengyuan.manage_os.old.ying.erpm.data.MenuItem;
import com.shengyuan.manage_os.old.ying.erpm.db.Actual_Data_Table;
import com.shengyuan.manage_os.old.ying.erpm.layout.GridAdapter;
import com.shengyuan.manage_os.old.ying.erpm.layout.PageIndicator;
import com.shengyuan.manage_os.old.ying.erpm.layout.Workspace;
import com.shengyuan.manage_os.old.ying.erpm.layout.Workspace.OnViewChangedListener;
import com.shengyuan.manage_os.old.ying.erpm.util.Utility;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.widget.GridView;


public class FinishedListActivity extends Activity implements OnViewChangedListener {
	public static WorkSpaceActivity Instance;
    protected Workspace mWorkspace;
    protected PageIndicator mIndicator;
    
    public int mVerticalSpacing = 30;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ziyoujixie);
        mWorkspace = (Workspace) findViewById(R.id.workspace);
        mIndicator = (PageIndicator) findViewById(R.id.indicator);
        LoadMenuItems();
    }
    
    protected void LoadMenuItems() {
    	int k =30;
	    List<MenuItem> MenuItems = LoadMenuListItems(); 
	    
        if (MenuItems.size()==0) {
        	Utility.MessageBox(this,"没有可打印的凭条!");
        	this.finish();
		}else {
			//mWorkspace.removeAllViewsInLayout();
	        for (int i = 0; i < Math.ceil(1.0f * MenuItems.size() / Workspace.APP_PAGE_SIZE); i++) {
	            GridView grid = new GridView(this);
	            grid.setNumColumns(1);
	            grid.setHorizontalSpacing(5);
	            grid.setVerticalSpacing(k);
	            grid.setPadding(30, 50, 30, 20);
	            grid.setGravity(Gravity.CENTER);
	            grid.setAdapter(new GridAdapter(this, MenuItems, i));
	            mWorkspace.addView(grid);
	        }
	        mWorkspace.setOnViewChangedListener(this);
	        mIndicator.setIndication(mWorkspace.getChildCount(), 0);
		}	
	}
    
    @Override
    public void onChange(int cnt, int index) {
        mIndicator.setIndication(cnt, index);
    }

	private ArrayList<String> Json1, Json2;
	private ArrayList<Integer>  mRowid;
	private ArrayList<Integer>  mPrindCount;
	private ArrayList<String>  mOverTime;

	public List<MenuItem> LoadMenuListItems() 
    {
    	MenuItem item;
    	List<MenuItem> list = new ArrayList<MenuItem>();
		Actual_Data_Table dbo = new Actual_Data_Table(this);
		Json1 = dbo.getArray_AllFinished_ForPrinter(1);
		Json2 = dbo.Json2;
		mRowid = dbo.RowID;
		mPrindCount = dbo.PrintCount;
		mOverTime = dbo.OverTime;
		dbo.close();  
    	for (int i = 0; i < Json1.size(); i++) {
    		item = new MenuItem();
			item.title = GetCarName( Json1.get(i));
			item.index = i;
			list.add( item);
		} 
    	return list;
    }
	

	private String GetCarName(String json1) {
		String name = "", value="";
		JSONObject jObject ;
		try {
			jObject = new JSONObject(json1);
			name = getString(R.string.title_actual_form_editText_car);
			value = jObject.getString( name );
		} catch (Exception e) {
			
		}
		try{
			if (value.equals("")) {
				jObject = new JSONObject(json1);
				name = getString(R.string.title_actual_form_edittext_jixiemingcheng);
				value = jObject.getString( name );
			}
			
		} catch (Exception e) {
			value ="不是运输,机械单据";
		}
		return value;
	}

    private Handler mHandler= new Handler(){

		@Override
		public void handleMessage(Message msg) {
    	    Intent intent = new Intent();
    	    Actual_Form_PrintFinished.RowId = mRowid.get(msg.what);
    	    Actual_Form_PrintFinished.Json1 = Json1.get(msg.what);
    	    Actual_Form_PrintFinished.Json2 = Json2.get(msg.what);
    	    Actual_Form_PrintFinished.mPrindCount = mPrindCount.get(msg.what);
    	    Actual_Form_PrintFinished.overtime = mOverTime.get(msg.what);
    	    intent.setClass(FinishedListActivity.this, Actual_Form_PrintFinished.class);
    	    startActivityForResult(intent, 1);
		}
    	
    };
    public Handler GetHandler(){
    	return mHandler;
    }

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK ) {
			//mWorkspace.removeAllViews();
			//LoadMenuItems();
			finish();
		}
	}
}